#-*-coding:UTF-8-*-

import unittest

from ..ldap import *
from ..logger import logger


def test_LDAP():
    print_LDAP_info()
    server_ip, user_id, passwd, port, baseDN = get_LDAP_info()
    server=Server(server_ip,port=int(port))
    conn=Connection(server,user=user_id,password=passwd)
    print(server)
    print(conn)
    print(conn.bind())
    #print(server.info
    #print(server.schema
    print(conn.extend.standard.who_am_i())
    print(hasattr(conn,'entries'))
    print('----- test searching hanmj1 -----')
    conn.search('ou=users,'+baseDN,'(&(uid=hanmj1)(objectclass=person))',attributes=['uid','mail','userPassword'])
    if hasattr(conn,'entries'):
        ens=conn.entries
        for e in ens:
            print('*'*20)
            print(e)
            print(e['userPassword'])
            print(type(e['userPassword']))
            #print(dir(e['userPassword']))
            print(check_password(str(e['userPassword']),'hanmj1'))
            #print(dir(e))
    print('----- end searching hanmj1 -----')


class LDAPTest(unittest.TestCase):

    def setUp(self):
        pass

    def test_conn(self):
        logger.debug('\n')
        logger.debug('test_LDAP()')
        test_LDAP()
        logger.debug('print LDAP user')
        for i, v in enumerate(get_LDAP_user_list()):
            logger.debug('%s = %s'%(i, v))
        logger.debug('\n')
        logger.debug('print_all_LDAP_users')        
        print_all_LDAP_users()

        logger.debug('\n')
        print "user_existed('zhaoxp') = ", user_existed('zhaoxp')
        print "user_existed('zhaoxp2') = ", user_existed('zhaoxp2')
        logger.debug('\n')
        print "create_user('test1', 't1 name', 't1@xyz.com', 't1pass') =",create_user('test1', 't1 name', 't1@xyz.com', 't1pass')
        print "create_user('test2', 't2 name', 't2@xyz.com', 't2pass') =",create_user('test2', 't2 name', 't2@xyz.com', 't2pass')
        print "create_user('test3', 't3 name', 't3@xyz.com', 't3pass') =",create_user('test3', 't3 name', 't3@xyz.com', 't3pass')

    def test_get_info(self):
        pass

    def tearDown(self):
        pass